<?php

class SON_Forms_Admin extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');
        
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');
        $this->addElement($id);
    
        $nome = new Zend_Form_Element_Text('nome');
        $nome->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true, array('messages' => 'Informe um nome'))
            ->setAttrib('class', 'form-control')
            ->setAttrib('placeholder', 'Nome')
        	->setAttrib('required', 'required');
        $this->addElement($nome);
    
        $email = new Zend_Form_Element_Text('email');
        $email->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', true, array('messages' => 'Informe um e-mail'))
            ->addValidator('EmailAddress', true, array('messages' => 'E-mail inválido'))
            ->addValidator('Db_NoRecordExists', false, array('table' => 'admin', 'field' => 'email', 'messages' => 'E-mail já cadastrado'))
            ->setAttrib('class', 'form-control')
            ->setAttrib('placeholder', 'E-mail')
        	->setAttrib('required', 'required');
        $this->addElement($email);
    
        $senha = new Zend_Form_Element_Password('senha');
        $senha->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty', false, array('messages' => 'Informe uma senha'))
            ->setAttrib('class', 'form-control')
            ->setAttrib('placeholder', 'Senha')
        	->setAttrib('required', 'required');
        $this->addElement($senha);
    
        $id->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $nome->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $email->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
        $senha->removeDecorator('HtmlTag')->removeDecorator('Label')->removeDecorator('Errors');
    }
    
    public function setAsEditForm(array $data)
    {
        $this->populate($data);
        
        $this->getElement('email')
            ->getValidator('Db_NoRecordExists')
            ->setExclude(array('field' => 'id', 'value' => $data['id']));
        
        $this->getElement('senha')
            ->setRequired(false)
            ->removeValidator('NotEmpty')
        	->setAttrib('required', null);
        
        return $this;
    }
}